package com.examples {
	import com.greensock.TweenMax;

	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Item extends Sprite {
		private	var _xml : XML;
		private	var _xmlList : XMLList;
		private	var _xmlLoader : URLLoader = new URLLoader();
		private	var _container : MovieClip = new MovieClip();
		private	var _imageLoader : Loader;

		public function Item() {
			makeItem();
			addEventListener(MouseEvent.MOUSE_OVER, itemOver);
			addEventListener(MouseEvent.MOUSE_OUT, itemOut);
		}

		public function makeItem() : void {
			_xmlLoader.load(new URLRequest("xml/images.xml"));
			_xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
		}

		private function xmlLoaded(event : Event) : void {
			_xml = XML(event.target.data);
			_xmlList = _xml.children();
			_imageLoader = new Loader();
			_imageLoader.load(new URLRequest(_xmlList[0].attribute("thumb")));
			_container.buttonMode = true;
			_container.addChild(_imageLoader);
			addChild(_container);
		}

		public function itemOver(event : MouseEvent) : void {
			TweenMax.to(event.currentTarget, .5, {alpha:0.5, tint:0xf5f7d3});
		}

		public function itemOverHandlerAdd() : void {
			addEventListener(MouseEvent.MOUSE_OVER, itemOver);
			TweenMax.to(this, .3, {alpha:1});
			buttonMode = true;
		}

		public function itemOverHandlerRemove() : void {
			removeEventListener(MouseEvent.MOUSE_OVER, itemOver);
			TweenMax.to(_container, .3, {alpha:.5});
			_container.buttonMode = false;
		}

		public function itemOut(event : MouseEvent) : void {
			TweenMax.to(event.currentTarget, .2, {alpha:1, tint:null});
		}

		public function itemDispose() : void {
			removeEventListener(MouseEvent.MOUSE_OUT, itemOut);
			TweenMax.to(_container, .3, {alpha:1, tint:null});
			_container.buttonMode = false;
		}

		public function itemAdd() : void {
			addEventListener(MouseEvent.MOUSE_OUT, itemOut);
			addEventListener(MouseEvent.MOUSE_OVER, itemOver);
			TweenMax.to(this, .3, {alpha:1});
			buttonMode = true;
		}
	}
}
